﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class MainUI : IUIBase
{
    private Button btnFunc1;

    private Button btnFunc2;

    private Button btnFunc3;

    private Button btnSpeed;
    private Text txtSpeed;

    private Button btnPause;
    private Text txtPause;

    private Button btnReStart;

    private Slider sliderEnergy;

    private Text txtEnergy;

    private int maxEnergy = 0;
    private int curEnergy = 0;

    public MainUI() : base("UI/MainUI")
    {
        btnFunc1 = gameObjUI.GetComponent<Button>("BtnFunc1");
        btnFunc1.onClick.AddListener(OnClickBtnFunc1);

        btnFunc2 = gameObjUI.GetComponent<Button>("BtnFunc2");
        btnFunc2.onClick.AddListener(OnClickBtnFunc2);

        btnFunc3 = gameObjUI.GetComponent<Button>("BtnFunc3");
        btnFunc3.onClick.AddListener(OnClickBtnFunc3);

        btnSpeed = gameObjUI.GetComponent<Button>("BtnSpeed");
        btnSpeed.onClick.AddListener(OnClickBtnSpeed);

        txtSpeed = gameObjUI.GetComponent<Text>("BtnSpeed/Text");

        btnPause = gameObjUI.GetComponent<Button>("BtnPause");
        btnPause.onClick.AddListener(OnClickBtnPause);

        txtPause = gameObjUI.GetComponent<Text>("BtnPause/Text");

        btnReStart = gameObjUI.GetComponent<Button>("BtnReStart");
        btnReStart.onClick.AddListener(OnClickReStart);

        sliderEnergy = gameObjUI.GetComponent<Slider>("Energy");
        txtEnergy = gameObjUI.GetComponent<Text>("Energy/Text");

        SetMaxEnergy(20);
        SetCurEnergy(13);
    }

    public void OnClickBtnFunc1()
    {
        if (curEnergy > 2)
        {
            ChangeEnergy(-2);

            // 点击按钮创建一只怪物
            MonsterObject boss = StageManager.Create<MonsterObject>(StageManager.GetInstance().GetCurrentStage(), 1);
            boss.SetPosition(new Vector3(-4, -2.2f, 0));
            boss.GetModule().Get<ModAttr>().SetMapHp(50);
            boss.GetModule().Get<ModAttr>().SetCurHp(50);
            boss.GetModule().Get<ModAttr>().SetSpeed(2);
            boss.GetModule().Get<ModAttr>().SetGroup(ModAttr.GROUP_RED);
            boss.Start();
        } 
    }

    public void OnClickBtnFunc2()
    {
        
    }

    public void OnClickBtnFunc3()
    {
        if (curEnergy > 3)
        {
            ChangeEnergy(-3);

            // 点击按钮创建一只怪物
            MonsterObject boss = StageManager.Create<MonsterObject>(StageManager.GetInstance().GetCurrentStage(), 2);
            boss.SetPosition(new Vector3(-4, -2.2f, 0));
            boss.GetModule().Get<ModAttr>().SetMapHp(50);
            boss.GetModule().Get<ModAttr>().SetCurHp(50);
            boss.GetModule().Get<ModAttr>().SetSpeed(2);
            boss.GetModule().Get<ModAttr>().SetGroup(ModAttr.GROUP_RED);
            boss.Start();
        }
    }

    public void OnClickBtnSpeed()
    {
        if (txtSpeed.text == "1X")
        {
            txtSpeed.text = "2X";
            Time.timeScale = 2;
        }
        else if (txtSpeed.text == "2X")
        {
            txtSpeed.text = "3X";
            Time.timeScale = 3;
        }
        else
        {
            txtSpeed.text = "1X";
            Time.timeScale = 1;
        }
    }

    public void OnClickBtnPause()
    {
        if (txtPause.text == "▶")
        {
            txtPause.text = "||";
            Time.timeScale = 1;
        }
        else
        {
            txtPause.text = "▶";
            Time.timeScale = 0;
        }
    }

    public void OnClickReStart()
    {
        GameUtils.ClearAllMonster(StageManager.GetInstance().GetCurrentStage());

        ResetFunc();
    }

    public override void Show()
    {
        base.Show();

        ResetFunc();
    }

    private void ResetFunc()
    {
        // 恢复速度，取消暂停
        Time.timeScale = 1;
        txtSpeed.text = "1X";
        txtPause.text = "||";
    }

    public void SetMaxEnergy(int value)
    {
        maxEnergy = value;

        UpdateSliderEnergy();
    }

    public void SetCurEnergy(int value)
    {
        curEnergy = value;

        if (curEnergy > maxEnergy)
        {
            curEnergy = maxEnergy;
        }
        else if (curEnergy < 0)
        {
            curEnergy = 0;
        }

        UpdateSliderEnergy();
    }

    public void ChangeEnergy(int value)
    {
        curEnergy += value;

        if (curEnergy > maxEnergy)
        {
            curEnergy = maxEnergy;
        }
        else if (curEnergy < 0)
        {
            curEnergy = 0;
        }

        UpdateSliderEnergy();
    }

    private void UpdateSliderEnergy()
    {
        if (maxEnergy <= 0)
        {
            return;
        }

        sliderEnergy.value = curEnergy / (float)maxEnergy;
        txtEnergy.text = curEnergy.ToString() + "/" + maxEnergy.ToString();
    }
}
